import "./controller";
import "./global";
import events from "./events";
import apis from "./utils/backend";
import { config } from "./global";
import * as rpcHandlers from "./rpcHandlers";
import { initRPCService } from "@common/utils/initRPCService";

class App {
  constructor() {
    this.initEvents();
  }

  initEvents() {
    events.Global.SXT.Talk.Select.on(
      async ({ authorization, customerId, porchUserId, targetId }) => {
        if (targetId !== config.PAGE_ID) return;

        await apis.sxt.loadTalkMessages({
          authorization,
          customerId,
          limit: 20,
          porchUserId,
        });
      },
    );
    events.Global.SXT.Talk.Message.Send.on(async (params) => {
      if (params.targetId !== config.PAGE_ID) return;

      await apis.sxt.sendTalkMessage({
        authorization: params.authorization,
        senderPorchId: params.senderPorchId,
        type: params.type,
        content: params.content,
        receiverId: params.receiverId,
      });
    });
  }
}

initRPCService(
  events.Global,
  rpcHandlers,
  `inject[${config.PLUGIN_ID}:${config.PAGE_ID}]`,
);
new App();
